package cn.tedu.boot44.controller;

import cn.tedu.boot44.entity.Job;
import cn.tedu.boot44.entity.Logistics;
import cn.tedu.boot44.entity.Sales;
import cn.tedu.boot44.entity.Technology;
import cn.tedu.boot44.mapper.SalMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Classname: SalController
 * @Author: bromide
 * @CreateTime: 2022/5/14--16:35
 * @Version: V1.0
 * @Description:
 */
@RestController
public class SalController {
    @Autowired(required = false)
    SalMapper mapper;

    @RequestMapping("/insert")
    public void insert(@RequestBody Job job){
        System.out.println("==============="+job);
        //插入业务   通过 job 中的info判断职业
        String info = job.getInfo();
        System.out.println("====================得到的职业:"+info);
        if (info.equals("销售")){
            Sales sales = new Sales(job.getId(),job.getName(),job.getBaseSal(),job.getSaleCount(),
                    job.getBonus(),job.getEndSal());
            mapper.insertSales(sales);
        }else if (info.equals("技术")){
            Technology tech = new Technology(job.getId(),job.getName(),job.getBaseSal(),
                    job.getScore(),job.getBonus(),job.getEndSal());
            mapper.insertTech(tech);
        }else if (info.equals("后勤")){
            Logistics log = new Logistics(job.getId(),job.getName(),job.getBaseSal(),
                    job.getBonus(),job.getEndSal());
            mapper.insertLog(log);
        }
    }

    @RequestMapping("/selectAvgSal")
    public double selectAvgSal(){
        List<Double> list = mapper.selectBonus();
        double avg = 0;
        for (int i = 0; i < list.size(); i++) {
            avg+=list.get(i);
        }
        System.out.println("奖金总和:"+avg);
        avg/=list.size();
        System.out.println("平均奖金:"+avg);

        //查出来 并返回
        return avg;
    }

}
